﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Restaurant.Biz;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// AddRoomWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddRoomWindow : AddModeWindowBase
    {
        public AddRoomWindow()
        {
            InitializeComponent();
        }

        public RS_Room Room
        {
            get { return m_entity as RS_Room; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            Room = new RS_Room();
            Room.Tables = new List<RS_Table>();
        }

        protected override void InitializeForUpdating()
        {
            btnContinue.Visibility = Visibility.Hidden;
        }

        protected override void AddData()
        {
            Room.RoomType = (int)(RoomType)Enum.Parse(typeof(RoomType), cbbRoomType.SelectedItem.ToString());

            switch (m_crud)
            {
                case CrudType.Create:
                    Room.RoomId = Aide.LocalClient.Invoke<int>(
                        service => service.AddRoom(Room));
                    break;
                case CrudType.Update:
                    EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_Room));
                    delta.DeltaProperties.Add("RoomName", Room.RoomName);
                    delta.DeltaProperties.Add("RoomType", Room.RoomType);
                    delta.Conditions.Add("RoomId", Room.RoomId);

                    Aide.LocalClient.Invoke<int>(
                        service => service.SetRoom(delta));
                    break;
                default:
                    break;
            }

            base.AddData();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string[] names = Enum.GetNames(typeof(RoomType));
            string[] items = new string[names.Length - 1];
            Array.Copy(names, items, names.Length - 1);

            cbbRoomType.ItemsSource = items;
            cbbRoomType.SelectedIndex = 0;
        }
    }
}
